package de.edb.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import de.edb.model.Artist;
import de.edb.model.Event;

public class ArtistDao extends HibernateDaoSupport {
	private EventDao eventDao;
	
	public void setEventDao(EventDao eventDao) {
		this.eventDao = eventDao;
	}

	/**
	 * Creates and saves a {@link Artist} object.
	 * @param name
	 * @param description
	 * @param genre
	 * @throws DaoException
	 */
	public void create(String name, String description, String genre) throws DaoException {
		Artist a = new Artist();
		a.setName(name);
		a.setDescription(description);
		a.setGenre(genre);
		save(a);
	}

	/**
	 * returns a single artists by its primary db key
	 * 
	 * @param id
	 * @return the Artist
	 */
	public Artist findById(int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Artist) template.get(Artist.class, id);
	}

	
	/**
	 * saves a {@link Artist} object.
	 * @param artist
	 * @return
	 * @throws DaoException
	 */
	public Artist save(Artist artist) throws DaoException {
		if (artist.getName().isEmpty())
    	{
    		throw new DaoException("Die Pflichtfelder müssen ausgefüllt werden");
    	} else {
			HibernateTemplate template = getHibernateTemplate();
			template.saveOrUpdate(artist);
			return artist;
    	}
	}

	
	/**
	 * deletes a {@link Artist} object.
	 * @param artist
	 * @throws DaoException
	 */
	public void delete(Artist artist) throws DaoException {
		HibernateTemplate template = getHibernateTemplate();
		Set<Event> artistEvents = eventDao.findByArtist(artist);
		if (artistEvents.size() == 0) {
			template.delete(artist);
		} else if(artistEvents.size() == 1){
			throw new DaoException("Der Künstler kann nicht gelöscht werden, da er bereits an einem Event teilnimmt.");
		} else {
			throw new DaoException("Der Künstler kann nicht gelöscht werden, da er bereits an " + artistEvents.size() + " Events teilnimmt.");
		}
		
	}


	/**
	 * @return all Artist objects from the database.
	 */
	@SuppressWarnings("unchecked")
	public List<Artist> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		@SuppressWarnings("rawtypes")
		List results = template.loadAll(Artist.class);
		return results;
	}
	
	/**
	 * @return Artist objects filtered by name from the database.
	 */
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Artist> findByName(final String keyword) {
		HibernateTemplate template = getHibernateTemplate();
		return (List<Artist>) template.execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(Artist.class);
				criteria.add(Restrictions.ilike("name", keyword, MatchMode.ANYWHERE));
				List<Artist> artists = criteria.list();
				return artists;
			}
		});
	}


}
